//
//  Extension+FLAnimatedImageView.swift
//  yktIcve
//
//  Created by 尤增强 on 2019/5/23.
//  Copyright © 2019 zqyou. All rights reserved.
//

import UIKit
import SDWebImage
 extension FLAnimatedImageView {

    //设置图片包括GIF
    func setFLAnimatedImageView(str:String){

        self.setFLAnimatedImageView(str: str, placeholder:"default_slider_img.png")
    }

    //设置图片包括GIF
    func setFLAnimatedImageView(str:String,placeholder:String){
        if let image = UIImage(named: "default_slider_img.png"){
            self.setFLAnimatedImageView(str: str, img:image )
        }
    }


    //设置图片包括GIF
    func setFLAnimatedImageView(str:String,img:UIImage){
        self.isHidden = false
      
        self.sd_setImage(with: URL(string: str), placeholderImage: img)
    }

    ///设置图片,更新请求成功后的状态
    func setFLAnimatedImageView(str:String,beginContentMode:UIView.ContentMode,endContentMode:UIView.ContentMode){
        //获取本地默认动画图片
        self.contentMode = beginContentMode
        if let path = Bundle.main.path(forResource: "loading", ofType: ".gif"){
            let data = NSData(contentsOfFile: path)
            self.isHidden = false
            let urls = str.components(separatedBy: "?")
            self.sd_setImage(with: URL(string: urls[0]), placeholderImage: UIImage.sd_animatedGIF(with: data! as Data), options: SDWebImageOptions.refreshCached) { (img, error, cacheType, url) in
                if img != nil{
                    self.contentMode = endContentMode
                }
            }
        }else{
            if let image = UIImage(named: "default_slider_img.png"){
                self.setFLAnimatedImageView(str: str, img:image )
            }
        }
    }

}
